<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="学号" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="姓名" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <!-- <el-input v-model="queryParams.college" placeholder="请输入系部" clearable @keyup.enter.native="handleQuery" /> -->
            <el-form-item label="系部" prop="college">
                <el-select v-model="queryParams.college" placeholder="请选择系部" >
                 <el-option 
                 v-for="item in unitNameList" 
                 :key="item.sponsorId"
                 :label="item.sponsorName"
                 :value="item.sponsorName"
                 >{{item.sponsorName}}</el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="学期" prop="schoolYear">
                <!-- <el-input v-model="queryParams.schoolYear" placeholder="请输入学期" clearable
                    @keyup.enter.native="handleQuery" /> -->
                    <el-select v-model="queryParams.schoolYear"  placeholder="请选择学期" >
                    <el-option
                     v-for="item in schoolYearList"
                     :key="item.schoolYearId"
                     :label="item.schoolYearId"
                     :value="item.schoolYearId"
                    />
                    </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!--  <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['reportcard:card:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                  v-hasPermi="['reportcard:card:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                  @click="handleDelete" v-hasPermi="['reportcard:card:remove']">删除</el-button>
          </el-col>  -->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['reportcard:card:export']">导出学分</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="成绩单id" align="center" prop="id" /> -->
            <el-table-column label="学号" align="center" prop="userName" width="150" />
            <!-- <el-table-column label="姓名" align="center" prop="nickName" /> -->
            <el-table-column label="姓名" align="center" prop="nickName" />
            <el-table-column label="班级" align="center" prop="className" />
            <el-table-column label="系部" align="center" prop="college" />
            <el-table-column label="学期" align="center" prop="schoolYear" />
            <el-table-column label="社团组织" align="center" prop="massOrg">
                <template slot-scope="scope">
                    {{ scope.row.massOrg }}分<br />
                    <!-- <a @click="scoreDetail(scope.row, 1)">查看详情</a> -->
                    <el-button size="mini" type="text" icon="el-icon-document"
                        @click="scoreDetail(scope.row, 1)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="文体活动" align="center" prop="culturalActivity">
                <template slot-scope="scope">
                    {{ scope.row.culturalActivity }}分<br />
                    <!-- <a @click="scoreDetail(scope.row, 1)">查看详情</a> -->
                    <el-button size="mini" type="text" icon="el-icon-document"
                        @click="scoreDetail(scope.row, 2)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="主题教育" align="center" prop="themeEducation">
                <template slot-scope="scope">
                    {{ scope.row.themeEducation }}分<br />
                    <!-- <a @click="scoreDetail(scope.row, 1)">查看详情</a> -->
                    <el-button size="mini" type="text" icon="el-icon-document"
                        @click="scoreDetail(scope.row, 3)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="志愿服务" align="center" prop="volunteerActivity">
                <template slot-scope="scope">
                    {{ scope.row.volunteerActivity }}分<br />
                    <!-- <a @click="scoreDetail(scope.row, 1)">查看详情</a> -->
                    <el-button size="mini" type="text" icon="el-icon-document"
                        @click="scoreDetail(scope.row, 4)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="我的荣誉" align="center" prop="myHonor">
                <template slot-scope="scope">
                    {{ scope.row.myHonor }}分<br />
                    <!-- <a @click="scoreDetail(scope.row, 1)">查看详情</a> -->
                    <el-button size="mini" type="text" icon="el-icon-document"
                        @click="scoreDetail(scope.row, 5)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template slot-scope="scope">
                    <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['reportcard:card:edit']">修改</el-button> -->
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="goCardPdf(scope.row)"
                        v-hasPermi="['reportcard:card:detail']">成绩单详情</el-button>
                    <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="goCardPdf(scope.row)"
                        v-if="name==scope.row.userName">查看导出</el-button> -->
                    <!-- <el-button size="mini" type="text" icon="el-icon-download" @click="goCardPdf(scope.row,true)"
                        v-hasPermi="['reportcard:card:edit']">下载</el-button> -->
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['reportcard:card:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改二课成绩单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="学号" prop="userName">
                    <el-input v-model="form.userName" placeholder="请输入学号" />
                </el-form-item>
                <el-form-item label="姓名" prop="nickName">
                    <el-input v-model="form.nickName" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="系部" prop="college">
                    <el-input v-model="form.college" placeholder="请输入系部" />
                </el-form-item>
                <el-form-item label="学期" prop="schoolYear">
                    <el-input v-model="form.schoolYear" placeholder="请输入学期" />
                </el-form-item>
                <el-form-item label="德育" prop="moralEducation">
                    <el-input v-model="form.moralEducation" placeholder="请输入德育" />
                </el-form-item>
                <el-form-item label="智育" prop="intellectualEducation">
                    <el-input v-model="form.intellectualEducation" placeholder="请输入智育" />
                </el-form-item>
                <el-form-item label="美育" prop="aestheticEducation">
                    <el-input v-model="form.aestheticEducation" placeholder="请输入美育" />
                </el-form-item>
                <el-form-item label="体育" prop="sports">
                    <el-input v-model="form.sports" placeholder="请输入体育" />
                </el-form-item>
                <el-form-item label="劳育" prop="lawEducation">
                    <el-input v-model="form.lawEducation" placeholder="请输入劳育" />
                </el-form-item>
                <el-form-item label="团学组织" prop="leagueOrg">
                    <el-input v-model="form.leagueOrg" placeholder="请输入团学组织" />
                </el-form-item>
                <el-form-item label="校园活动" prop="campusActivity">
                    <el-input v-model="form.campusActivity" placeholder="请输入校园活动" />
                </el-form-item>
                <el-form-item label="社会实践" prop="socialPractice">
                    <el-input v-model="form.socialPractice" placeholder="请输入社会实践" />
                </el-form-item>
                <el-form-item label="志愿活动" prop="volunteerActivity">
                    <el-input v-model="form.volunteerActivity" placeholder="请输入志愿活动" />
                </el-form-item>
                <el-form-item label="劳动教育" prop="labourEducation">
                    <el-input v-model="form.labourEducation" placeholder="请输入劳动教育" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 分数详情 -->
        <el-dialog :title="title + '详情'" :visible.sync="detailOpen" width="1000px" append-to-body>
            <div slot="title">
                <span style="font-size:18px;">{{ title + '详情' }} </span>
                <el-button style="margin-left:20px;" type="success" plain icon="el-icon-download" size="mini"
                    @click="handleExport('detail')" v-hasPermi="['reportcard:card:export']">导出</el-button>
            </div>
            <el-table border :data="detailList">
                <el-table-column label="社团名称" align="center" prop="activityName" v-if="title == '社团组织'" />
                <el-table-column label="组织类型" align="center" prop="activityType" v-if="title == '社团组织'" />
                <el-table-column label="社团类型" align="center" prop="orgType" v-if="title == '社团组织'" />
                <!-- <el-table-column label="得分" align="center" prop="StartTime" v-if="title == '社团组织'"/> -->
                <el-table-column label="活动名称" align="center" prop="activityName"
                    v-if="title == '文体活动' || title == '主题教育' || title == '志愿服务'" />
                <el-table-column label="发起单位" align="center" prop="hostName"
                    v-if="title == '文体活动' || title == '主题教育' || title == '志愿服务'" />
                <el-table-column label="指导单位" align="center" prop="college" v-if="title == '我的荣誉'" />
                <el-table-column label="活动类型" align="center" prop="activityType"
                    v-if="title == '文体活动' || title == '主题教育' || title == '志愿服务'" />
                <el-table-column label="活动时间" align="center" prop="startTime"
                    v-if="title == '文体活动' || title == '主题教育' || title == '志愿服务'" />
                <el-table-column label="获奖时间" align="center" prop="joinDate" v-if="title == '我的荣誉'" />
                <el-table-column label="奖项名称" align="center" prop="project" v-if="title == '我的荣誉'" />
                <el-table-column label="得分" align="center" prop="mark"
                    v-if="title == '文体活动' || title == '主题教育' || title == '志愿服务' || title == '我的荣誉' || title == '社团组织'" />
            </el-table>
        </el-dialog>

    </div>
</template>

<script>
import { listCard, getCard, delCard, addCard, updateCard, getCardInfos } from "@/api/reportcard/card";
import axios from 'axios';
import { GetYearList } from '@/api/organ/organ';
import { listSponsor } from "@/api/system/sponsor";
import { mapGetters } from 'vuex';

export default {
    name: "Card",
    computed: {
        ...mapGetters(['name', 'userName']),
    },
    data() {
        return {
            unitNameList: [],//系部下拉
            schoolYearList: [],//学期选择
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 二课成绩单表格数据
            cardList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: null,
                nickName: null,
                college: null,
                schoolYear: null,
            },
            initinitialData:{},
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            detailOpen: false,
            detailList: [],

            currData:null,
        };
    },
    created() {
        this.getList();
         /** 学期选择 */ 
         GetYearList().then(response => { 
         this.schoolYearList = response.rows;
             for (let i  = 0; i  < response.total; i ++) {
            if(response.rows[i].defaultValue == "1"){
             console.log(response.rows[i],"year")
             this.queryParams.schoolYear = response.rows[i].schoolYearId;
         }
      }
    //   getTime(this.queryParams.schoolYear).then(res=>{
    //     this.queryParams.period = res.data[0].dictLabel
    //     this.timeList = res.data
    //     this.getList()
    //     this.canRead()
    //   })
    })
                /** 系部下拉框 */
              listSponsor().then(response => {
                  this.unitNameList = response.rows;
                  console.log(response.rows);
             });
    },
    methods: {
        /** 查询二课成绩单列表 */
        getList() {
            this.loading = true;
            listCard(this.queryParams).then(response => {
                console.log(response);
                this.cardList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 查看详情
        scoreDetail(row, type) {

            var typeValArr = ['qwe', "社团组织", "文体活动", "主题教育", "志愿服务", "我的荣誉"];
            this.detailOpen = true;
            console.log(type);
            this.title = typeValArr[type];
            console.log(this.title);
            this.currData = {
                studentId: row.userName,
                type
            }
            getCardInfos(this.currData).then(res => {
                console.log(res);
                this.detailList = res.data
            })

        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                userName: null,
                nickName: null,
                college: null,
                moralEducation: null,
                intellectualEducation: null,
                aestheticEducation: null,
                sports: null,
                lawEducation: null,
                leagueOrg: null,
                campusActivity: null,
                socialPractice: null,
                volunteerActivity: null,
                labourEducation: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
           this.queryParams.userName = ''
           this.queryParams.college = ''
           this.queryParams.nickName = ''
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加二课成绩单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getCard(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改二课成绩单";
            });
        },
        // 查看成绩单
        goCardPdf(row, isPdf) {
            const vm = this
            vm.$router.push({  //核心语句
                path: '/reportcard/cardPdf/index/' + row.userName, query: { isPdf: isPdf },//跳转的路径

            })
        },
        // 下载
        handleDownload() {
            // window.open(process.env.VUE_APP_BASE_API + `/二课成绩单.pdf`);
            // var codeUrl = '../../../../public/static/erkeCode.pdf';
            var codeUrl = require('../../../assets/files/erkeCode.pdf');
            if (codeUrl) {
                // this.download.mame(codeUrl);
                // this.$download.saveAs(codeUrl)
                // window.open(codeUrl);

                const that = this;
                var oReq = new XMLHttpRequest();
                var URL = codeUrl; // URL 为URL地址
                oReq.open("GET", URL, true);
                oReq.responseType = "arraybuffer";
                oReq.onload = function () {
                    var file = new Blob([oReq.response], {
                        type: "application/pdf;charset=utf-8",
                    });
                    that.$download.saveAs(file, "codePDF.pdf"); // that.name为文件名
                };
                oReq.send();
                //             const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' });
                //   this.$download.saveAs(blob, data.fileName);
            }
        },
        //文件下载
        handleDownload1() {
            axios('../../../../public/erkeCode.pdf', {
                responseType: 'blob', //重要代码
            }).then(res => {
                const url = window.URL.createObjectURL(new Blob([res.data]));
                const link = document.createElement('a');
                link.href = url;
                let fileName = "erkeCode" //保存到本地的文件名称
                link.setAttribute('download', fileName);
                document.body.appendChild(link);
                link.click();
            })
        },

        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCard(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCard(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除二课成绩单编号为"' + ids + '"的数据项？').then(function () {
                return delCard(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport(type) {
            if(type=='detail'){
                this.download('/reportcard/card/exportDetail', {
                   ...this.currData,
                   schoolYear
                }, `detailCard_${new Date().getTime()}.xlsx`)
            }else{
                this.download('/reportcard/card/export', {
                    ...this.queryParams
                }, `card_${new Date().getTime()}.xlsx`)
            }
        },
       
        
    }
};
</script>
